package com.example.composableexample.util

import android.os.Parcel
import android.os.Parcelable
import androidx.compose.foundation.border
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.itemsIndexed
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.Add
import androidx.compose.material3.Icon
import androidx.compose.material3.Surface
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.SideEffect
import androidx.compose.runtime.mutableStateListOf
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import androidx.lifecycle.viewmodel.compose.viewModel
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch

/**
 * author: LiLiang
 * createTime: 2022/9/22-12:59
 * remark:
 **/

@Preview
@Composable
fun Test() {
    val testModel: TestModel = viewModel()
    val users = testModel.datas
    SideEffect {
       testModel.getList()
   }
    Column {
        users.forEach {
            Text(text = it.name)
        }
    }
   

//    Surface(modifier = Modifier.fillMaxSize()) {
//        LazyColumn(content = {
//            itemsIndexed(model.datas) { index: Int, data: Int ->
//                Box(
//                    Modifier
//                        .padding(5.dp)
//                        .fillMaxWidth()
//                        .height(50.dp)
//                        .border(width = 1.dp, color = Color.Green)
//                        .clickable {
//                            model.datas.add(index + 1)
//                        },
//                    contentAlignment = Alignment.Center
//                ) {
//                    if (index == model.datas.size - 1) {
//                        Icon(
//                            imageVector = Icons.Default.Add,
//                            contentDescription = "",
//                            tint = Color.Red
//                        )
//                    } else {
//                        Text(text = data.toString())
//                    }
//                }
//            }
//        })
//    }
}

class TestModel : ViewModel(){

    val datas = mutableStateListOf<User>()
    fun getList() {
        viewModelScope.launch(Dispatchers.IO) {
            if (datas.isNotEmpty()) {
                datas.clear()
            }
            datas.addAll(list)
        }
    }
//    val datas = mutableListOf(0,1,2,3)

}

data class User(val id: Int = 0, val name: String = "")

val list = listOf(
    User(1, "java"),
    User(2, "Kotlin"),
    User(3, "Android"),
    User(4, "Compose"),
)
